"""
AI角色模型
对应Node.js项目中的AiPerson表，保持数据结构一致
"""

from sqlalchemy import Column, String, DateTime, Integer, Boolean, Text, JSON
from datetime import datetime

from .base import Base


class AiPerson(Base):
    """
    AI角色模型
    与Node.js项目中的ai_persons表保持一致
    """
    __tablename__ = "ai_persons"
    
    # 主键
    id = Column(Integer, primary_key=True, autoincrement=True, nullable=False, comment="AI角色ID")
    
    # 基本信息
    image = Column(String, nullable=False, comment="角色主图片")
    avatar = Column(String, nullable=False, comment="角色头像")
    username = Column(String, nullable=False, comment="角色用户名（中文）")
    username_en = Column(String, nullable=False, comment="角色用户名（英文）")
    username_jp = Column(String, nullable=False, comment="角色用户名（日文）")
    
    # 交互数据
    likes = Column(String, nullable=False, comment="点赞数")
    comments = Column(String, nullable=False, comment="评论数")
    
    # 高级功能标识
    is_premium = Column(Boolean, nullable=False, default=False, comment="是否为高级角色")
    
    # 描述信息（多语言）
    description = Column(Text, nullable=False, comment="角色描述（中文）")
    description_en = Column(Text, nullable=False, comment="角色描述（英文）")
    description_jp = Column(Text, nullable=False, comment="角色描述（日文）")
    
    # 媒体资源
    images = Column(JSON, nullable=False, comment="角色图片集合")
    videos = Column(JSON, nullable=True, comment="角色视频集合")
    
    # 角色档案（多语言）
    character_profile = Column(JSON, nullable=True, comment="角色档案（中文）")
    character_profile_en = Column(JSON, nullable=True, comment="角色档案（英文）")
    character_profile_jp = Column(JSON, nullable=True, comment="角色档案（日文）")
    
    # 角色类型
    type = Column(String, nullable=False, comment="角色类型")
    
    # Sequelize自动维护的时间戳
    created_at = Column(DateTime, nullable=False, default=datetime.utcnow, comment="创建时间")
    updated_at = Column(DateTime, nullable=False, default=datetime.utcnow, onupdate=datetime.utcnow, comment="更新时间")
    
    def __repr__(self):
        return f"<AiPerson(id={self.id}, username={self.username}, type={self.type})>"
    
    def get_character_profile_by_language(self, language: str = "zh") -> dict:
        """
        根据语言获取角色档案
        
        Args:
            language: 语言代码 ("zh", "en", "jp")
            
        Returns:
            dict: 对应语言的角色档案
        """
        if language == "en":
            return self.character_profile_en or self.character_profile
        elif language == "jp":
            return self.character_profile_jp or self.character_profile
        else:
            return self.character_profile
    
    def get_username_by_language(self, language: str = "zh") -> str:
        """
        根据语言获取角色用户名
        
        Args:
            language: 语言代码 ("zh", "en", "jp")
            
        Returns:
            str: 对应语言的用户名
        """
        if language == "en":
            return self.username_en
        elif language == "jp":
            return self.username_jp
        else:
            return self.username
    
    def get_description_by_language(self, language: str = "zh") -> str:
        """
        根据语言获取角色描述
        
        Args:
            language: 语言代码 ("zh", "en", "jp")
            
        Returns:
            str: 对应语言的描述
        """
        if language == "en":
            return self.description_en
        elif language == "jp":
            return self.description_jp
        else:
            return self.description 